package com.doit.day01;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.RegionLocator;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.HFileOutputFormat2;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

public class MyBulkLoad {
    public static void main(String[] args) throws Exception {

        System.setProperty("HADOOP_USER_NAME","root");


        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum","linux01:2181");

        //设置一大堆参数
        Job job = Job.getInstance(conf);

        //设置主类
        job.setJarByClass(MyBulkLoad.class);

        //设置map得类
        job.setMapperClass(MyMapper.class);

        //map输出key和value得类型
        job.setMapOutputKeyClass(ImmutableBytesWritable.class);
        job.setMapOutputValueClass(KeyValue.class);

        //设置hfile给定具体得表得元数据等信息
        Connection conn = ConnectionFactory.createConnection(conf);
        Table table = conn.getTable(TableName.valueOf("doit:user_info1"));
        RegionLocator regionLocator = conn.getRegionLocator(TableName.valueOf("doit:user_info1"));
        HFileOutputFormat2.configureIncrementalLoad(job,table,regionLocator);

        //设置输出文件得类型
        job.setOutputFormatClass(HFileOutputFormat2.class);

        //设置reduce task得个数
        job.setNumReduceTasks(0);

        //设置文件输入得路径
        FileInputFormat.setInputPaths(job,new Path("hdfs://linux01:8020/hbase.txt"));

        //文件输出得路径
        FileOutputFormat.setOutputPath(job,new Path("hdfs://linux01:8020/out/"));

        job.waitForCompletion(true);




    }
}
